// 订单数量随时间增长曲线图
<template>
    <div class="gradient-content full-width full-height">
        <div :id="idName"></div>
    </div>
</template>

<script>
import { Chart } from '@antv/g2'
export default {
    props: {
        idName: {
            type: String,
            default: 'orderNumber',
        },
        chartData: {
            type: Array,
            default: () => [],
        },
        height: {
            type: [String, Number],
            default: 300,
        }
    },
    data() {
        return {
            chart: null,
            // height: 0,
        }
    },
    watch: {
        chartData(val) {
            console.log('chartData', val);
            this.chart.changeData(val)
        },
    },
    mounted() {
        this.$nextTick(() => {
            // const boxContElement = document.querySelector(`.gradient-content`)
            // this.height = boxContElement.clientHeight

            this.render()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.destroy()
    },
    methods: {
        render() {
            // Step 1: 创建 Chart 对象
            this.chart = new Chart({
                container: this.idName,
                height: this.height,
                autoFit: true,
                padding: [30, 40, 30, 50],
            })
            // Step 2: 载入数据源
            this.chart.data(this.chartData)
            this.chart.axis(true)
            // Step 3：创建图形语法，绘制柱状图，由 quantity 和 date 两个属性决定图形位置，date 映射至 x 轴，quantity 映射至 y 轴
            this.chart.area().position('date*quantity').shape('smooth')
            this.chart
                .line()
                .position('date*quantity')
                .shape('smooth')
                .label('quantity', {
                    style: {
                        fill: '#fff',
                    },
                })

            this.chart.scale({
                quantity: {
                    // min: 100,
                    alias: '订单数量',
                },
                date: {
                    alias: '时间',
                    range: [0, 1],
                },
            })
            // Step 4: 渲染图表
            this.chart.render()
        },
    },
}
</script>
